// File: FlatLiner.java
//Purpose: This file illustrates the performance advantage gained
//         in the grid test by recognizing that the domain of the
//         problem is a slope of either 0 or Undefined (x/0).

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class FlatLiner implements LineDrawer {
	static int pix = 0xffffffff;
	static int[] pixa = new int[0];
	static int pixasz = 0;
	
	
	public void drawLine(BufferedImage bi, int x0, int y0, int x1, int y1){		
		if (x0 == x1) {
			int h = (y1-y0);
			if (h < 0) h = 0-h;
			h++;
			if (h>pixasz) {
		  		pixasz = h;
		  		pixa = new int[h];
		  		for (int i=0;i<pixasz;i++) pixa[i] = 0xffffffff;
			}  
			bi.setRGB(x0,y0,1,h,pixa,0,1);	
		} else if (y0 == y1) {	
			int w = x1-x0;
			if (w < 0) w = 0-w;
			w++;
			if (w>pixasz) {
		  		pixasz = w;
		  		pixa = new int[w];
		  		for (int i=0;i<pixasz;i++) pixa[i] = 0xffffffff;
			}  
			bi.setRGB(x0,y0,w,1,pixa,0,pixasz);	
		} else {
			throw (new RuntimeException("FlatLiner called on non-flat line"));
		}
	}	
}			